########################################################
## PROGRAM NAME: 008_persistence.R                    ##
## AUTHOR: MATT MLECZKO                               ##
## DATE CREATED: 05/05/2021                           ##
## INPUTS:                                            ##
##    002_af_wide_final.Rda                           ##
##                                                    ##
## OUTPUTS:                                           ##
##                                                    ## 
## PURPOSE: Analyze persistence of integration and    ##
##          segregation, create Figure 5              ##
##                                                    ##
## LIST OF UPDATES:                                   ##
##                                                    ##
########################################################

log <- file("008_persistence.txt") 
sink(log, append=TRUE)
sink(log, append=TRUE, type="message")

set.seed(08540)

## load libraries ##

library(tidycensus)
library(tidyverse)
library(foreign)
library(stringr)
library(tm)
library(gdata)
library(gsubfn)
library(readxl)
library(stargazer)
library(ggplot2)
library(haven)
library(webr)

## define paths ##
data_path <- "PATH TO DATA HERE"
progs <- "PATH TO PROGRAMS HERE"

## set working directory ##
setwd(data_path)

##
## load necessary functions ## 
##

`%notin%` <- Negate(`%in%`)
source(paste0(progs,"stata_merge.R"))

## load the analytic file ##
load("002_af_wide_final.Rda")

## initial analysis ## 

prop.table(table(af.wide.final$int_er_abs_2000))
prop.table(table(af.wide.final$int_er_rel_2000))
prop.table(table(af.wide.final$int_er_relf_2000))

prop.table(table(af.wide.final$int_er_abs_2010))
prop.table(table(af.wide.final$int_er_rel_2010))
prop.table(table(af.wide.final$int_er_relf_2010))

prop.table(table(af.wide.final$int_er_abs_2020))
prop.table(table(af.wide.final$int_er_rel_2020))
prop.table(table(af.wide.final$int_er_relf_2020))

prop.table(table(af.wide.final$int_jt_abs_2000))
prop.table(table(af.wide.final$int_jt_rel_a_2000))
prop.table(table(af.wide.final$int_jt_relf_a_2000))

prop.table(table(af.wide.final$int_jt_abs_2010))
prop.table(table(af.wide.final$int_jt_rel_a_2010))
prop.table(table(af.wide.final$int_jt_relf_a_2010))

prop.table(table(af.wide.final$int_jt_abs_2020))
prop.table(table(af.wide.final$int_jt_rel_a_2020))
prop.table(table(af.wide.final$int_jt_relf_a_2020))


## proceed ##

af.wide.final <- af.wide.final %>% 
  mutate(state = case_when(substr(GEOID,1,2) == "01" ~ "Alabama",
                                 substr(GEOID,1,2) == "02" ~ "Alaska",
                                 substr(GEOID,1,2) == "04" ~ "Arizona",
                                 substr(GEOID,1,2) == "05" ~ "Arkansas",
                                 substr(GEOID,1,2) == "06" ~ "California",
                                 substr(GEOID,1,2) == "08" ~ "Colorado",
                                 substr(GEOID,1,2) == "09" ~ "Connecticut",
                                 substr(GEOID,1,2) == "10" ~ "Delaware",
                                 substr(GEOID,1,2) == "11" ~ "District of Columbia",
                                 substr(GEOID,1,2) == "12" ~ "Florida",
                                 substr(GEOID,1,2) == "13" ~ "Georgia",
                                 substr(GEOID,1,2) == "15" ~ "Hawaii",
                                 substr(GEOID,1,2) == "16" ~ "Idaho",
                                 substr(GEOID,1,2) == "17" ~ "Illinois",
                                 substr(GEOID,1,2) == "18" ~ "Indiana",
                                 substr(GEOID,1,2) == "19" ~ "Iowa",
                                 substr(GEOID,1,2) == "20" ~ "Kansas",
                                 substr(GEOID,1,2) == "21" ~ "Kentucky",
                                 substr(GEOID,1,2) == "22" ~ "Louisiana",
                                 substr(GEOID,1,2) == "23" ~ "Maine",
                                 substr(GEOID,1,2) == "24" ~ "Maryland",
                                 substr(GEOID,1,2) == "25" ~ "Massachusetts",
                                 substr(GEOID,1,2) == "26" ~ "Michigan",
                                 substr(GEOID,1,2) == "27" ~ "Minnesota",
                                 substr(GEOID,1,2) == "28" ~ "Mississippi",
                                 substr(GEOID,1,2) == "29" ~ "Missouri",
                                 substr(GEOID,1,2) == "30" ~ "Montana",
                                 substr(GEOID,1,2) == "31" ~ "Nebraska",
                                 substr(GEOID,1,2) == "32" ~ "Nevada",
                                 substr(GEOID,1,2) == "33" ~ "New Hampshire",
                                 substr(GEOID,1,2) == "34" ~ "New Jersey",
                                 substr(GEOID,1,2) == "35" ~ "New Mexico",
                                 substr(GEOID,1,2) == "36" ~ "New York",
                                 substr(GEOID,1,2) == "37" ~ "North Carolina",
                                 substr(GEOID,1,2) == "38" ~ "North Dakota",
                                 substr(GEOID,1,2) == "39" ~ "Ohio",
                                 substr(GEOID,1,2) == "40" ~ "Oklahoma",
                                 substr(GEOID,1,2) == "41" ~ "Oregon",
                                 substr(GEOID,1,2) == "42" ~ "Pennsylvania",
                                 substr(GEOID,1,2) == "44" ~ "Rhode Island",
                                 substr(GEOID,1,2) == "45" ~ "South Carolina",
                                 substr(GEOID,1,2) == "46" ~ "South Dakota",
                                 substr(GEOID,1,2) == "47" ~ "Tennessee",
                                 substr(GEOID,1,2) == "48" ~ "Texas",
                                 substr(GEOID,1,2) == "49" ~ "Utah",
                                 substr(GEOID,1,2) == "50" ~ "Vermont",
                                 substr(GEOID,1,2) == "51" ~ "Virginia",
                                 substr(GEOID,1,2) == "53" ~ "Washington",
                                 substr(GEOID,1,2) == "54" ~ "West Virginia",
                                 substr(GEOID,1,2) == "55" ~ "Wisconsin",
                                 substr(GEOID,1,2) == "56" ~ "Wyoming"))

## get number of tracts by state ##

state.tracts <- af.wide.final %>%
  group_by(state) %>%
  summarize(ntracts = n()) 

## merge back to af.wide.final ## 

af.wide.final <- left_join(af.wide.final,
                           state.tracts,
                           "state")

## 1980 - 2000 file ##

load("003_8090_trts.Rda")

all.decades.m <- stata.merge(metro.tracts.8090.fin,
                             af.wide.final, 
                             "GEOID")

## check merge ##
table(all.decades.m$merge.variable, useNA = "ifany")

## keep matches ##
all.decades <- all.decades.m %>%
  filter(merge.variable == 3) %>%
  select(-merge.variable) %>%
  mutate(int_er_relf_1980 = ifelse(int_er_rel_1980 == 1 & 
                                     rowSums(cbind(per_black_1980, per_hl_1980), na.rm=T) >= 0.2, 1, 0),
         int_er_relf_1990 = ifelse(int_er_rel_1990 == 1 & 
                                     rowSums(cbind(per_black_1990, per_hl_1990), na.rm=T) >= 0.2, 1, 0),
         int_er_relf_2000 = ifelse(int_er_rel_2000 == 1 & 
                                     rowSums(cbind(per_black_2000, per_hl_2000), na.rm=T) >= 0.2, 1, 0))


####################
## JOINT ANALYSIS ##
####################

jt.out <- function(indata, var1, var2, var3, var4, var5, var6){
  
  ## 2000 integrated tracts ##
  
  integrated.trt.2000.jt <- indata %>%
    filter(get(var1) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2010 ## 
  
  integrated.trt.2010.jt <- indata %>%
    filter(get(var2) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2020 ##
  integrated.trt.2020.jt <- indata %>%
    filter(get(var3) == 1) %>%
    select(GEOID,
           state)
  
  ## stably segregated tracts ## 
  stable.seg.tracts.jt <- filter(indata, 
                                 get(var1) == 0,
                                 get(var2) == 0,
                                 get(var3) == 0)
  
  ## tracts that went from integrated to segregated ## 
  int.to.seg.tracts.jt <- indata %>%
    filter((get(var1) == 1 & get(var2) == 0 & get(var3) == 0) | 
           (get(var1) == 1 & get(var2) == 1 & get(var3) == 0))
  
  ## tracts that went from segregated to integrated ## 
  seg.to.int.tracts.jt <- indata %>%
    filter((get(var1) == 0 & get(var2) == 1 & get(var3) == 1) | 
           (get(var1) == 0 & get(var2) == 0 & get(var3) == 1))
  
  ## stably integrated tracts ## 
  stable.int.tracts.jt <- filter(indata, 
                                 get(var1) == 1 & 
                                 get(var2) == 1 & 
                                 get(var3) == 1)
  
  ## stably highly diverse tracts ##
  stable.hd.tracts.jt <- stable.int.tracts.jt %>%
    filter(get(var4) == 1,
           get(var5) == 1,
           get(var6) == 1,) %>%
    select(GEOID,
           state) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## regional analysis ##
  st.seg.st <- table(stable.seg.tracts.jt$state)
  st.seg.st.df <- as.data.frame(st.seg.st)
  names(st.seg.st.df) <- c("state","times")
  st.seg.st.df$rank <- rank(-st.seg.st.df$times,ties.method="min")
  st.seg.st.df <- st.seg.st.df[order(st.seg.st.df$rank,decreasing = F),]
  st.seg.st.df
  
  st.seg.st.df.fin <- left_join(st.seg.st.df,
                                state.tracts,
                                "state")
  
  st.seg.st.df.fin$pct <- st.seg.st.df.fin$times/st.seg.st.df.fin$ntracts
  
  st.int.st <- table(stable.int.tracts.jt$state)
  st.int.st.df <- as.data.frame(st.int.st)
  names(st.int.st.df) <- c("state","times")
  st.int.st.df$rank <- rank(-st.int.st.df$times,ties.method="min")
  st.int.st.df <- st.int.st.df[order(st.int.st.df$rank,decreasing = F),]
  st.int.st.df
  
  st.int.st.df.fin <- left_join(st.int.st.df,
                                state.tracts,
                                "state")
  
  st.int.st.df.fin$pct <- st.int.st.df.fin$times/st.int.st.df.fin$ntracts

  int.to.seg.st <- table(int.to.seg.tracts.jt$state)
  int.to.seg.st.df <- as.data.frame(int.to.seg.st)
  names(int.to.seg.st.df) <- c("state","times")
  int.to.seg.st.df$rank <- rank(-int.to.seg.st.df$times,ties.method="min")
  int.to.seg.st.df <- int.to.seg.st.df[order(int.to.seg.st.df$rank,decreasing = F),]
  int.to.seg.st.df

  int.to.seg.st.df.fin <- left_join(int.to.seg.st.df,
                                    state.tracts,
                                    "state")

  int.to.seg.st.df.fin$pct <- int.to.seg.st.df.fin$times/int.to.seg.st.df.fin$ntracts

  seg.to.int.st <- table(seg.to.int.tracts.jt$state)
  seg.to.int.st.df <- as.data.frame(seg.to.int.st)
  names(seg.to.int.st.df) <- c("state","times")
  seg.to.int.st.df$rank <- rank(-seg.to.int.st.df$times,ties.method="min")
  seg.to.int.st.df <- seg.to.int.st.df[order(seg.to.int.st.df$rank,decreasing = F),]
  seg.to.int.st.df

  seg.to.int.st.df.fin <- left_join(seg.to.int.st.df,
                                    state.tracts,
                                   "state")

  seg.to.int.st.df.fin$pct <- seg.to.int.st.df.fin$times/seg.to.int.st.df.fin$ntracts


  st.seg.st.df.fin$pct <- st.seg.st.df.fin$times/st.seg.st.df.fin$ntracts

  ## stable high diversity ##

  st.hd.st <- table(stable.hd.tracts.jt$state)
  st.hd.st.df <- as.data.frame(st.hd.st)
  names(st.hd.st.df) <- c("state","times")
  st.hd.st.df$rank <- rank(-st.hd.st.df$times,ties.method="min")
  st.hd.st.df <- st.hd.st.df[order(st.hd.st.df$rank,decreasing = F),]
  st.hd.st.df

  st.hd.st.df.fin <- left_join(st.hd.st.df,
                               state.tracts,
                               "state")

  st.hd.st.df.fin$pct <- st.hd.st.df.fin$times/st.hd.st.df.fin$ntracts



  ## create table ## 

  tract.type.jt <- c("Persistently segregated",
                     "Integrated to segregated",
                     "Segregated to integrated",
                     "Persistently integrated",
                     "Persistently highly diverse")

   dfs.jt <- list(stable.seg.tracts.jt,
                  int.to.seg.tracts.jt,
                  seg.to.int.tracts.jt,
                  stable.int.tracts.jt,
                  stable.hd.tracts.jt)
   
   num.jt <- sapply(dfs.jt, function(x) nrow(x))

  num.n.jt <- as.numeric(as.character(num.jt))

  t.stats.table.jt <- as.data.frame(cbind(tract.type.jt, num.n.jt))

  t.stats.table.pr.jt <- t.stats.table.jt %>%
    mutate(prev.by.type = num.jt/nrow(indata)) %>%
    mutate(prev.by.type = as.numeric(format(prev.by.type, scientific = F)))
  
  return(t.stats.table.pr.jt)
  
}


jt.0020.prim <- jt.out(af.wide.final,
                       "int_jt_relf_a_2000",
                       "int_jt_relf_a_2010",
                       "int_jt_relf_a_2020",
                       "hd_jt_2000",
                       "hd_jt_2010",
                       "hd_jt_2020")

jt.0020.abs <- jt.out(af.wide.final,
                      "int_jt_abs_2000",
                      "int_jt_abs_2010",
                      "int_jt_abs_2020",
                      "hd_jt_2000",
                      "hd_jt_2010",
                      "hd_jt_2020")

jt.0020.rel <- jt.out(af.wide.final,
                      "int_jt_rel_a_2000",
                      "int_jt_rel_a_2010",
                      "int_jt_rel_a_2020",
                      "hd_jt_2000",
                      "hd_jt_2010",
                      "hd_jt_2020")

jt.0020.abs <- jt.out(af.wide.final,
                      "int_jt_abs_2000",
                      "int_jt_abs_2010",
                      "int_jt_abs_2020",
                      "hd_jt_2000",
                      "hd_jt_2010",
                      "hd_jt_2020")

jt.0020.rel <- jt.out(af.wide.final,
                      "int_jt_rel_a_2000",
                      "int_jt_rel_a_2010",
                      "int_jt_rel_a_2020",
                      "hd_jt_2000",
                      "hd_jt_2010",
                      "hd_jt_2020")


#################
## ER ANALYSIS ##
#################

er.out <- function(indata, var1, var2, var3, var4, var5, var6){
  
  ## 2000 integrated tracts ##
  
  integrated.trt.2000.er <- indata %>%
    filter(get(var1) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2010 ## 
  
  integrated.trt.2010.er <- indata %>%
    filter(get(var2) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2020 ##
  integrated.trt.2020.er <- indata %>%
    filter(get(var3) == 1) %>%
    select(GEOID,
           state)
  
  ## stably segregated tracts ## 
  stable.seg.tracts.er <- filter(indata, 
                                 get(var1) == 0,
                                 get(var2) == 0,
                                 get(var3) == 0)
  
  ## stably segregated White tracts ## 
  
  stable.seg.white.tracts <- stable.seg.tracts.er %>%
    filter(maj_race_2000 == "per_white" & 
           maj_race_2010 == "per_white" & 
           maj_race_2020 == "per_white") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated Black tracts ## 
  
  stable.seg.black.tracts <- stable.seg.tracts.er %>%
    filter(maj_race_2000 == "per_black",
           maj_race_2010 == "per_black",
           maj_race_2020 == "per_black") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated Latino tracts ## 
  
  stable.seg.latinx.tracts <- stable.seg.tracts.er %>%
    filter(maj_race_2000 == "per_hl",
           maj_race_2010 == "per_hl",
           maj_race_2020 == "per_hl") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.seg.latinx.tracts$FIPS)
  
  ## stably segregated Asian tracts ## 
  
  stable.seg.asian.tracts <- stable.seg.tracts.er %>%
    filter(maj_race_2000 == "per_asian",
           maj_race_2010 == "per_asian",
           maj_race_2020 == "per_asian") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.seg.asian.tracts$FIPS)
  
  ## all other stably segregated tracts ##
  
  stable.seg.other.tracts <- stable.seg.tracts.er %>%
    filter(GEOID %notin% c(stable.seg.asian.tracts$GEOID,
                           stable.seg.black.tracts$GEOID,
                           stable.seg.latinx.tracts$GEOID,
                           stable.seg.white.tracts$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  #print("MULTIPLE STABLE SEG NHS")
  #print(nrow(stable.seg.other.tracts))
  
  ## collect GEOIDS ## 
  seg.GEOIDs <- c(stable.seg.white.tracts$GEOID,
                  stable.seg.black.tracts$GEOID,
                  stable.seg.latinx.tracts$GEOID,
                  stable.seg.asian.tracts$GEOID)
  
  check1 <- filter(stable.seg.tracts.er, 
                   GEOID %notin% seg.GEOIDs)
  
  ## tracts that went from integrated to segregated ## 
  int.to.seg.tracts.er <- indata %>%
    filter((get(var1) == 1 & get(var2) == 0 & get(var3) == 0) | 
             (get(var1) == 1 & get(var2) == 1 & get(var3) == 0))
  
  table(int.to.seg.tracts.er$maj_race_2000)
  table(int.to.seg.tracts.er$maj_race_2010)
  table(int.to.seg.tracts.er$maj_race_2020)
  
  ## transition to White segregated ## 
  
  to.wh.seg <- int.to.seg.tracts.er %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_white" & 
            get(var3) == 0 & maj_race_2020 == "per_white") |
           (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_white")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.wh.seg$FIPS)
  
  ## transition to Black segregated ## 
  
  to.bl.seg <- int.to.seg.tracts.er %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_black" & 
              get(var3) == 0 & maj_race_2020 == "per_black") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_black")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.bl.seg$FIPS)
  
  ## transition to Latinx segregated ## 
  
  to.lx.seg <- int.to.seg.tracts.er %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_hl" & 
              get(var3) == 0 & maj_race_2020 == "per_hl") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_hl")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.lx.seg$FIPS)
  
  ## transition to Asian segregated ## 
  
  to.as.seg <- int.to.seg.tracts.er %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_asian" & 
              get(var3) == 0 & maj_race_2020 == "per_asian") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_asian")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.as.seg$FIPS)
  
  ## all other int to seg tracts ##
  
  to.oth.seg <- int.to.seg.tracts.er %>%
    filter(GEOID %notin% c(to.as.seg$GEOID,
                           to.bl.seg$GEOID,
                           to.lx.seg$GEOID,
                           to.wh.seg$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  #print("MULTIPLE TO SEG NHS")
  #print(nrow(to.oth.seg))

  ## collect GEOIDS ## 
  int.to.seg.GEOIDs <- c(to.wh.seg$GEOID,
                         to.bl.seg$GEOID,
                         to.lx.seg$GEOID,
                         to.as.seg$GEOID)
  
  check2 <- filter(int.to.seg.tracts.er, 
                   GEOID %notin% int.to.seg.GEOIDs)
  
  table(check2$maj_race_2000)
  table(check2$maj_race_2010)
  table(check2$maj_race_2020)
  
  ## tracts that went from segregated to integrated ## 
  seg.to.int.tracts.er <- indata %>%
    filter((get(var1) == 0 & get(var2) == 1 & get(var3) == 1) | 
             (get(var1) == 0 & get(var2) == 0 & get(var3) == 1))
  
  table(seg.to.int.tracts.er$maj_race_2000)
  table(seg.to.int.tracts.er$maj_race_2010)
  table(seg.to.int.tracts.er$maj_race_2020)
  
  ## transition to White integrated ## 
  
  to.wh.int <- seg.to.int.tracts.er %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_white" & 
            get(var3) == 1 & maj_race_2020 == "per_white") |
           (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_white")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.wh.int$FIPS)
  
  ## transition to Black integrated ## 
  
  to.bl.int <- seg.to.int.tracts.er %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_black" & 
              get(var3) == 1 & maj_race_2020 == "per_black") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_black")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.bl.int$FIPS)
  
  
  ## transition to Latinx integrated ## 
  
  to.lx.int <- seg.to.int.tracts.er %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_hl" & 
              get(var3) == 1 & maj_race_2020 == "per_hl") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_hl")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.lx.int$FIPS)
  
  ## transition to Asian integrated ## 
  
  to.as.int <- seg.to.int.tracts.er %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_asian" & 
            get(var3) == 1 & maj_race_2020 == "per_asian") |
           (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_asian")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.as.int$FIPS)
  
  ## all other seg to int tracts ##
  
  to.oth.int <- seg.to.int.tracts.er %>%
    filter(GEOID %notin% c(to.as.int$GEOID,
                           to.bl.int$GEOID,
                           to.lx.int$GEOID,
                           to.wh.int$GEOID) & 
             maj_race_2000 %notin% c("per_aian","per_other") & 
             maj_race_2010 %notin% c("per_aian","per_other") & 
             maj_race_2020 %notin% c("per_aian","per_other")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## collect GEOIDS ## 
  seg.to.int.GEOIDs <- c(to.wh.int$GEOID,
                         to.bl.int$GEOID,
                         to.lx.int$GEOID,
                         to.as.int$GEOID)
  
  
  check3 <- filter(seg.to.int.tracts.er, 
                   GEOID %notin% seg.to.int.GEOIDs)
  
  table(check3$maj_race_2000)
  table(check3$maj_race_2010)
  table(check3$maj_race_2020)
  
  ## stably integrated tracts ## 
  
  stable.int.tracts.er <- filter(indata, 
                                 get(var1) == 1 & 
                                   get(var2) == 1 & 
                                   get(var3) == 1)
  
  table(stable.int.tracts.er$maj_race_2000)
  table(stable.int.tracts.er$maj_race_2010)
  table(stable.int.tracts.er$maj_race_2020)
  
  ## stably integrated White tracts ## 
  
  stable.int.white.tracts <- stable.int.tracts.er %>%
    filter(maj_race_2000 == "per_white",
           maj_race_2010 == "per_white",
           maj_race_2020 == "per_white") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.seg.white.tracts$FIPS)
  
  ## stably integrated Black tracts ## 
  
  stable.int.black.tracts <- stable.int.tracts.er %>%
    filter(maj_race_2000 == "per_black",
           maj_race_2010 == "per_black",
           maj_race_2020 == "per_black") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.black.tracts$FIPS)
  
  
  ## stably integrated Latinx tracts ## 
  
  stable.int.latinx.tracts <- stable.int.tracts.er %>%
    filter(maj_race_2000 == "per_hl",
           maj_race_2010 == "per_hl",
           maj_race_2020 == "per_hl") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.latinx.tracts$FIPS)
  
  
  ## stably integrated Asian tracts ## 
  
  stable.int.asian.tracts <- stable.int.tracts.er %>%
    filter(maj_race_2000 == "per_asian",
           maj_race_2010 == "per_asian",
           maj_race_2020 == "per_asian") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.asian.tracts$FIPS)
  
  ## all other stably int tracts ##
  
  stable.int.other.tracts <- stable.int.tracts.er %>%
    filter(GEOID %notin% c(stable.int.asian.tracts$GEOID,
                           stable.int.black.tracts$GEOID,
                           stable.int.latinx.tracts$GEOID,
                           stable.int.white.tracts$GEOID) & 
             maj_race_2000 %notin% c("per_aian","per_other") & 
             maj_race_2010 %notin% c("per_aian","per_other") & 
             maj_race_2020 %notin% c("per_aian","per_other")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  
  ## stably highly diverse tracts ## 
  
  stable.hd.tracts <- stable.int.tracts.er %>%
    filter(get(var4) == 1,
           get(var5) == 1,
           get(var6) == 1,) %>%
    select(GEOID,
           state) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.hd.tracts$FIPS)
  
  
  ## regional analysis ##
  
  st.seg.st.er <- table(stable.seg.tracts.er$state)
  st.seg.st.er.df <- as.data.frame(st.seg.st.er)
  names(st.seg.st.er.df) <- c("state","times")
  st.seg.st.er.df$rank <- rank(-st.seg.st.er.df$times,ties.method="min")
  st.seg.st.er.df <- st.seg.st.er.df[order(st.seg.st.er.df$rank,decreasing = F),]
  st.seg.st.er.df
  
  st.seg.st.er.df.fin <- left_join(st.seg.st.er.df,
                                   state.tracts,
                                   "state")
  
  st.seg.st.er.df.fin$pct <- st.seg.st.er.df.fin$times/st.seg.st.er.df.fin$ntracts
  
  st.int.st.er <- table(stable.int.tracts.er$state)
  st.int.st.er.df <- as.data.frame(st.int.st.er)
  names(st.int.st.er.df) <- c("state","times")
  st.int.st.er.df$rank <- rank(-st.int.st.er.df$times,ties.method="min")
  st.int.st.er.df <- st.int.st.er.df[order(st.int.st.er.df$rank,decreasing = F),]
  st.int.st.er.df
  
  st.int.st.er.df.fin <- left_join(st.int.st.er.df,
                                   state.tracts,
                                   "state")
  
  st.int.st.er.df.fin$pct <- st.int.st.er.df.fin$times/st.int.st.er.df.fin$ntracts
  
  int.to.seg.st.er <- table(int.to.seg.tracts.er$state)
  int.to.seg.st.er.df <- as.data.frame(int.to.seg.st.er)
  names(int.to.seg.st.er.df) <- c("state","times")
  int.to.seg.st.er.df$rank <- rank(-int.to.seg.st.er.df$times,ties.method="min")
  int.to.seg.st.er.df <- int.to.seg.st.er.df[order(int.to.seg.st.er.df$rank,decreasing = F),]
  int.to.seg.st.er.df
  
  int.to.seg.st.er.df.fin <- left_join(int.to.seg.st.er.df,
                                       state.tracts,
                                       "state")
  
  int.to.seg.st.er.df.fin$pct <- int.to.seg.st.er.df.fin$times/int.to.seg.st.er.df.fin$ntracts
  
  seg.to.int.st.er <- table(seg.to.int.tracts.er$state)
  seg.to.int.st.er.df <- as.data.frame(seg.to.int.st.er)
  names(seg.to.int.st.er.df) <- c("state","times")
  seg.to.int.st.er.df$rank <- rank(-seg.to.int.st.er.df$times,ties.method="min")
  seg.to.int.st.er.df <- seg.to.int.st.er.df[order(seg.to.int.st.er.df$rank,decreasing = F),]
  seg.to.int.st.er.df
  
  seg.to.int.st.er.df.fin <- left_join(seg.to.int.st.er.df,
                                       state.tracts,
                                       "state")
  
  seg.to.int.st.er.df.fin$pct <- seg.to.int.st.er.df.fin$times/seg.to.int.st.er.df.fin$ntracts
  
  
  ## stable high diversity ##
  
  st.hd.st.er <- table(stable.hd.tracts$state)
  st.hd.st.er.df <- as.data.frame(st.hd.st.er)
  names(st.hd.st.er.df) <- c("state","times")
  st.hd.st.er.df$rank <- rank(-st.hd.st.er.df$times,ties.method="min")
  st.hd.st.er.df <- st.hd.st.er.df[order(st.hd.st.er.df$rank,decreasing = F),]
  st.hd.st.er.df
  
  st.hd.st.er.df.fin <- left_join(st.hd.st.er.df,
                                  state.tracts,
                                  "state")
  
  st.hd.st.er.df.fin$pct <- st.hd.st.er.df.fin$times/st.hd.st.er.df.fin$ntracts
  
  
  ## create table ## 
  
  tract.type <- c("Persistently segregated",
                  "Persistently Asian segregated",
                  "Persistently Black segregated",
                  "Persistenly Latino segregated",
                  "Persistently white segregated",
                  "Persistently multiple segregated",
                  "Integrated to segregated",
                  "Integrated to Asian segregated",
                  "Integrated to Black segregated",
                  "Integrated to Hispanic/Latino segregated",
                  "Integrated to white segregated",
                  "Integrated to multiple segregated",
                  "Segregated to integrated",
                  "Segregated to Asian integrated",
                  "Segregated to Black integrated",
                  "Segregated to Hispanic/Latino integrated",
                  "Segregated to white integrated",
                  "Segregated to multiple integrated",
                  "Persistently integrated",
                  "Persistently Asian integrated",
                  "Persistently Black integrated",
                  "Persistently Latino integrated",
                  "Persistently white integrated",
                  "Persistently multiple integrated",
                  "Persistently highly diverse")
  
  dfs <- list(stable.seg.tracts.er,
              stable.seg.asian.tracts,
              stable.seg.black.tracts,
              stable.seg.latinx.tracts,
              stable.seg.white.tracts,
              stable.seg.other.tracts,
              int.to.seg.tracts.er,
              to.as.seg,
              to.bl.seg,
              to.lx.seg,
              to.wh.seg,
              to.oth.seg,
              seg.to.int.tracts.er,
              to.as.int,
              to.bl.int,
              to.lx.int,
              to.wh.int,
              to.oth.int,
              stable.int.tracts.er,
              stable.int.asian.tracts,
              stable.int.black.tracts,
              stable.int.latinx.tracts,
              stable.int.white.tracts,
              stable.int.other.tracts,
              stable.hd.tracts)
  
  ## what other trajectories are missing? ##
  
  cb <- bind_rows(dfs)
  
  missing <- indata %>%
    filter(GEOID %notin% cb$GEOID) %>%
    mutate(inv = paste0(get(var1),
                        get(var2),
                        get(var3)))
  
  print("OTHER TRAJECTORIES (INT = 1)")
  print(table(missing$inv, useNA = "ifany"))
  
  ## proceed ##
  
  num <- sapply(dfs, function(x) nrow(x))
  
  num.n <- as.numeric(as.character(num))
  
  t.stats.table <- as.data.frame(cbind(tract.type, num.n))
  
  table(af.wide.final$int_er_rel_2020)
  
  seg <- table(af.wide.final$int_er_rel_2020)[0]
  
  int <- table(af.wide.final$int_er_rel_2020)[1]
  
  t.stats.table.pr <- t.stats.table %>%
    mutate(prev.by.type = num/nrow(af.wide.final)) %>%
    mutate(prev.by.type = case_when(tract.type %in% c("Persistently Asian segregated",
                                                      "Persistently Black segregated",
                                                      "Persistenly Latino segregated",
                                                      "Persistently white segregated",
                                                      "Persistently multiple segregated") ~ num/nrow(stable.seg.tracts.er), 
                                    tract.type %in% c("Integrated to Asian segregated",
                                                      "Integrated to Black segregated",
                                                      "Integrated to Hispanic/Latino segregated",
                                                      "Integrated to white segregated",
                                                      "Integrated to multiple segregated") ~ num/nrow(int.to.seg.tracts.er), 
                                    tract.type %in% c("Segregated to Asian integrated",
                                                      "Segregated to Black integrated",
                                                      "Segregated to Hispanic/Latino integrated",
                                                      "Segregated to white integrated",
                                                      "Segregated to multiple integrated") ~ num/nrow(seg.to.int.tracts.er), 
                                    tract.type %in% c("Persistently Asian integrated",
                                                      "Persistently Black integrated",
                                                      "Persistently Latino integrated",
                                                      "Persistently white integrated",
                                                      "Persistently multiple integrated") ~ num/nrow(stable.int.tracts.er),
                                    TRUE ~ prev.by.type)) %>%
    mutate(prev.by.race = case_when(str_detect(tract.type, "Asian") ~ num/table(indata$maj_race_2020)["per_asian"],
                                    str_detect(tract.type, "Black") ~ num/table(indata$maj_race_2020)["per_black"],
                                    str_detect(tract.type, "Latino") ~ num/table(indata$maj_race_2020)["per_hl"],
                                    str_detect(tract.type, "white") ~ num/table(indata$maj_race_2020)["per_white"])) %>%
    mutate(prev.by.type = as.numeric(format(prev.by.type, scientific = F)))
  
  t.stats.table.pr$prev.by.type <- round(t.stats.table.pr$prev.by.type, 3)
  t.stats.table.pr$prev.by.race <- round(t.stats.table.pr$prev.by.race, 3)
  
  return(t.stats.table.pr)
  
}



er.0020.prim <- er.out(af.wide.final,
                       "int_er_relf_2000",
                       "int_er_relf_2010",
                       "int_er_relf_2020",
                       "hd_er_2000",
                       "hd_er_2010",
                       "hd_er_2020")

er.0020.rel <- er.out(af.wide.final,
                       "int_er_rel_2000",
                       "int_er_rel_2010",
                       "int_er_rel_2020",
                       "hd_er_2000",
                       "hd_er_2010",
                       "hd_er_2020")

er.0020.abs <- er.out(af.wide.final,
                      "int_er_abs_2000",
                      "int_er_abs_2010",
                      "int_er_abs_2020",
                      "hd_er_2000",
                      "hd_er_2010",
                      "hd_er_2020")

## now, 1980-2000 ## 
## need to do some reprocessing ##

all.decades.rf <- all.decades %>%
  select(-int_er_relf_2010,
         -int_er_relf_2020,
         -int_er_rel_2010,
         -int_er_rel_2020,
         -int_er_abs_2010,
         -int_er_abs_2020,
         -maj_race_2010,
         -maj_race_2020,
         -hd_er_2010,
         -hd_er_2020) %>%
  rename(int_er_relf_2020 = int_er_relf_2000,
         int_er_relf_2010 = int_er_relf_1990,
         int_er_relf_2000 = int_er_relf_1980,
         int_er_rel_2020 = int_er_rel_2000,
         int_er_rel_2010 = int_er_rel_1990,
         int_er_rel_2000 = int_er_rel_1980,
         int_er_abs_2020 = int_er_abs_2000,
         int_er_abs_2010 = int_er_abs_1990,
         int_er_abs_2000 = int_er_abs_1980,
         maj_race_2020 = maj_race_2000,
         maj_race_2010 = maj_race_1990,
         maj_race_2000 = maj_race_1980,
         hd_er_2020 = hd_er_2000,
         hd_er_2010 = hd_er_1990,
         hd_er_2000 = hd_er_1980) %>%
  mutate(maj_race_2000 = case_when(maj_race_2000 == "per_asianpl_1980" ~ "per_asian",
                                   maj_race_2000 == "per_black_1980" ~ "per_black",
                                   maj_race_2000 == "per_hl_1980" ~ "per_hl",
                                   maj_race_2000 == "per_white_1980" ~ "per_white",
                                   maj_race_2000 == "per_other_1980" ~ "per_other"),
         maj_race_2010 = case_when(maj_race_2010 == "per_asianpi_1990" ~ "per_asian",
                                   maj_race_2010 == "per_black_1990" ~ "per_black",
                                   maj_race_2010 == "per_hl_1990" ~ "per_hl",
                                   maj_race_2010 == "per_white_1990" ~ "per_white",
                                   maj_race_2010 == "per_other_1990" ~ "per_other",
                                   maj_race_2010 == "per_aian_1990" ~ "per_aian"))

er.8000.prim <- er.out(all.decades.rf,
                       "int_er_relf_2000",
                       "int_er_relf_2010",
                       "int_er_relf_2020",
                       "hd_er_2000",
                       "hd_er_2010",
                       "hd_er_2020")

er.8000.rel <- er.out(all.decades.rf,
                      "int_er_rel_2000",
                      "int_er_rel_2010",
                      "int_er_rel_2020",
                      "hd_er_2000",
                      "hd_er_2010",
                      "hd_er_2020")

er.8000.abs <- er.out(all.decades.rf,
                      "int_er_abs_2000",
                      "int_er_abs_2010",
                      "int_er_abs_2020",
                      "hd_er_2000",
                      "hd_er_2010",
                      "hd_er_2020")

##################
## SES ANALYSIS ##
##################


ses.out <- function(indata, var1, var2, var3, var4, var5, var6){
  
  ## 2000 integrated tracts ##
  
  integrated.trt.2000.ses <- indata %>%
    filter(get(var1) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2010 ## 
  
  integrated.trt.2010.ses <- indata %>%
    filter(get(var2) == 1) %>%
    select(GEOID,
           state)
  
  ## integrated tracts 2020 ##
  integrated.trt.2020.ses <- indata %>%
    filter(get(var3) == 1) %>%
    select(GEOID,
           state)
  
  ## stably segregated tracts ## 
  stable.seg.tracts.ses <- filter(indata, 
                                 get(var1) == 0,
                                 get(var2) == 0,
                                 get(var3) == 0)
  
  ## stably segregated LI tracts ## 
  
  stable.seg.li.tracts <- stable.seg.tracts.ses %>%
    filter(maj_ses_2000 == "linc",
           maj_ses_2010 == "linc",
           maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated MI tracts ## 
  
  stable.seg.mi.tracts <- stable.seg.tracts.ses %>%
    filter(maj_ses_2000 == "minc",
           maj_ses_2010 == "minc",
           maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.seg.mi.tracts$FIPS)
  
  ## stably segregated HI tracts ## 
  
  stable.seg.hi.tracts <- stable.seg.tracts.ses %>%
    filter(maj_ses_2000 == "hinc",
           maj_ses_2010 == "hinc",
           maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.seg.hi.tracts$FIPS)
  
  ## all other stably segregated tracts ##
  
  stable.seg.other.tracts <- stable.seg.tracts.ses %>%
    filter(GEOID %notin% c(stable.seg.li.tracts$GEOID,
                           stable.seg.mi.tracts$GEOID,
                           stable.seg.hi.tracts$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## collect GEOIDS ## 
  seg.GEOIDs <- c(stable.seg.li.tracts$GEOID,
                  stable.seg.mi.tracts$GEOID,
                  stable.seg.hi.tracts$GEOID,
                  stable.seg.other.tracts$GEOID)
  
  check1 <- filter(stable.seg.tracts.ses, 
                   GEOID %notin% seg.GEOIDs)
  
  ## tracts that went from integrated to segregated ## 
  int.to.seg.tracts.ses <- indata %>%
    filter((get(var1) == 1 & get(var2) == 0 & get(var3) == 0) | 
           (get(var1) == 1 & get(var2) == 1 & get(var3) == 0))
  
  table(int.to.seg.tracts.ses$maj_ses_2000)
  table(int.to.seg.tracts.ses$maj_ses_2010)
  table(int.to.seg.tracts.ses$maj_ses_2020)
  
  ## transition to LI segregated ## 
  
  to.li.seg <- int.to.seg.tracts.ses %>%
    filter((get(var2) == 0 & maj_ses_2010 == "linc" & 
              get(var3) == 0 & maj_ses_2020 == "linc") |
             (get(var2) == 1 & get(var3) == 0 & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.li.seg$FIPS)
  
  ## transition to MI segregated ## 
  
  to.mi.seg <- int.to.seg.tracts.ses %>%
    filter((get(var2) == 0 & maj_ses_2010 == "minc" & 
              get(var3) == 0 & maj_ses_2020 == "minc") |
             (get(var2) == 1 & get(var3) == 0 & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.mi.seg$FIPS)

  ## transition to HI segregated ## 
  
  to.hi.seg <- int.to.seg.tracts.ses %>%
    filter((get(var2) == 0 & maj_ses_2010 == "hinc" & 
              get(var3) == 0 & maj_ses_2020 == "hinc") |
             (get(var2) == 1 & get(var3) == 0 & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.hi.seg$FIPS)
  
  ## all other tracts ##
  
  to.other.seg <- int.to.seg.tracts.ses %>%
    filter(GEOID %notin% c(to.li.seg$GEOID,
                           to.mi.seg$GEOID,
                           to.hi.seg$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## collect GEOIDS ## 
  int.to.seg.GEOIDs <- c(to.li.seg$GEOID,
                         to.mi.seg$GEOID,
                         to.hi.seg$GEOID,
                         to.other.seg$GEOID)
  
  check2 <- filter(int.to.seg.tracts.ses, 
                   GEOID %notin% int.to.seg.GEOIDs)
  
  table(check2$maj_ses_2000)
  table(check2$maj_ses_2010)
  table(check2$maj_ses_2020)
  
  ## tracts that went from segregated to integrated ## 
  seg.to.int.tracts.ses <- indata %>%
    filter((get(var1) == 0 & get(var2) == 1 & get(var3) == 1) | 
           (get(var1) == 0 & get(var2) == 0 & get(var3) == 1))
  
  table(seg.to.int.tracts.ses$maj_ses_2000)
  table(seg.to.int.tracts.ses$maj_ses_2010)
  table(seg.to.int.tracts.ses$maj_ses_2020)
  
  ## transition to LI integrated ## 
  
  to.li.int <- seg.to.int.tracts.ses %>%
    filter((get(var2) == 1 & maj_ses_2010 == "linc" & 
              get(var3) == 1 & maj_ses_2020 == "linc") |
             (get(var2) == 0 & get(var3) == 1 & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.li.int$FIPS)
  
  ## transition to MI integrated ## 
  
  to.mi.int <- seg.to.int.tracts.ses %>%
    filter((get(var2) == 1 & maj_ses_2010 == "minc" & 
              get(var3) == 1 & maj_ses_2020 == "minc") |
             (get(var2) == 0 & get(var3) == 1 & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.mi.int$FIPS)
  
  ## transition to HI integrated ## 
  
  to.hi.int <- seg.to.int.tracts.ses %>%
    filter((get(var2) == 1 & maj_ses_2010 == "hinc" & 
              get(var3) == 1 & maj_ses_2020 == "hinc") |
             (get(var2) == 0 & get(var3) == 1 & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(to.hi.int$FIPS)
  
  ## all other tracts ##
  
  to.other.int <- seg.to.int.tracts.ses %>%
    filter(GEOID %notin% c(to.li.int$GEOID,
                           to.mi.int$GEOID,
                           to.hi.int$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  
  ## collect GEOIDS ## 
  seg.to.int.GEOIDs <- c(to.li.int$GEOID,
                         to.mi.int$GEOID,
                         to.hi.int$GEOID,
                         to.other.int$GEOID)
  
  
  check3 <- filter(seg.to.int.tracts.ses, 
                   GEOID %notin% seg.to.int.GEOIDs)
  
  table(check3$maj_ses_2000)
  table(check3$maj_ses_2010)
  table(check3$maj_ses_2020)
  
  ## stably integrated tracts ## 
  
  stable.int.tracts.ses <- filter(indata, 
                                 get(var1) == 1 & 
                                   get(var2) == 1 & 
                                   get(var3) == 1)
  
  table(stable.int.tracts.ses$maj_ses_2000)
  table(stable.int.tracts.ses$maj_ses_2010)
  table(stable.int.tracts.ses$maj_ses_2020)
  
  ## stably integrated LI tracts ## 

  stable.int.li.tracts <- stable.int.tracts.ses %>%
    filter(maj_ses_2000 == "linc",
           maj_ses_2010 == "linc",
           maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.li.tracts$FIPS)
  
  
  ## stably integrated MI tracts ## 
  
  stable.int.mi.tracts <- stable.int.tracts.ses %>%
    filter(maj_ses_2000 == "mi",
           maj_ses_2010 == "mi",
           maj_ses_2020 == "mi") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.mi.tracts$FIPS)
  
  ## stably integrated HI tracts ## 
  
  stable.int.hi.tracts <- stable.int.tracts.ses %>%
    filter(maj_ses_2000 == "hinc",
           maj_ses_2010 == "hinc",
           maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.int.hi.tracts$FIPS)
  
  ## all other stably segregated tracts ##
  
  stable.int.other.tracts <- stable.int.tracts.ses %>%
    filter(GEOID %notin% c(stable.int.li.tracts$GEOID,
                           stable.int.mi.tracts$GEOID,
                           stable.int.hi.tracts$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably highly diverse tracts ## 
  
  stable.hd.tracts <- stable.int.tracts.ses %>%
    filter(get(var4) == 1,
           get(var5) == 1,
           get(var6) == 1,) %>%
    select(GEOID,
           state) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  table(stable.hd.tracts$FIPS)
  
  
  ## regional analysis ##
  
  st.seg.st.ses <- table(stable.seg.tracts.ses$state)
  st.seg.st.ses.df <- as.data.frame(st.seg.st.ses)
  names(st.seg.st.ses.df) <- c("state","times")
  st.seg.st.ses.df$rank <- rank(-st.seg.st.ses.df$times,ties.method="min")
  st.seg.st.ses.df <- st.seg.st.ses.df[order(st.seg.st.ses.df$rank,decreasing = F),]
  st.seg.st.ses.df
  
  st.seg.st.ses.df.fin <- left_join(st.seg.st.ses.df,
                                   state.tracts,
                                   "state")
  
  st.seg.st.ses.df.fin$pct <- st.seg.st.ses.df.fin$times/st.seg.st.ses.df.fin$ntracts
  
  st.int.st.ses <- table(stable.int.tracts.ses$state)
  st.int.st.ses.df <- as.data.frame(st.int.st.ses)
  names(st.int.st.ses.df) <- c("state","times")
  st.int.st.ses.df$rank <- rank(-st.int.st.ses.df$times,ties.method="min")
  st.int.st.ses.df <- st.int.st.ses.df[order(st.int.st.ses.df$rank,decreasing = F),]
  st.int.st.ses.df
  
  st.int.st.ses.df.fin <- left_join(st.int.st.ses.df,
                                   state.tracts,
                                   "state")
  
  st.int.st.ses.df.fin$pct <- st.int.st.ses.df.fin$times/st.int.st.ses.df.fin$ntracts
  
  int.to.seg.st.ses <- table(int.to.seg.tracts.ses$state)
  int.to.seg.st.ses.df <- as.data.frame(int.to.seg.st.ses)
  names(int.to.seg.st.ses.df) <- c("state","times")
  int.to.seg.st.ses.df$rank <- rank(-int.to.seg.st.ses.df$times,ties.method="min")
  int.to.seg.st.ses.df <- int.to.seg.st.ses.df[order(int.to.seg.st.ses.df$rank,decreasing = F),]
  int.to.seg.st.ses.df
  
  int.to.seg.st.ses.df.fin <- left_join(int.to.seg.st.ses.df,
                                       state.tracts,
                                       "state")
  
  int.to.seg.st.ses.df.fin$pct <- int.to.seg.st.ses.df.fin$times/int.to.seg.st.ses.df.fin$ntracts
  
  seg.to.int.st.ses <- table(seg.to.int.tracts.ses$state)
  seg.to.int.st.ses.df <- as.data.frame(seg.to.int.st.ses)
  names(seg.to.int.st.ses.df) <- c("state","times")
  seg.to.int.st.ses.df$rank <- rank(-seg.to.int.st.ses.df$times,ties.method="min")
  seg.to.int.st.ses.df <- seg.to.int.st.ses.df[order(seg.to.int.st.ses.df$rank,decreasing = F),]
  seg.to.int.st.ses.df
  
  seg.to.int.st.ses.df.fin <- left_join(seg.to.int.st.ses.df,
                                       state.tracts,
                                       "state")
  
  seg.to.int.st.ses.df.fin$pct <- seg.to.int.st.ses.df.fin$times/seg.to.int.st.ses.df.fin$ntracts
  
  
  ## stable high diversity ##
  
  st.hd.st.ses <- table(stable.hd.tracts$state)
  st.hd.st.ses.df <- as.data.frame(st.hd.st.ses)
  names(st.hd.st.ses.df) <- c("state","times")
  st.hd.st.ses.df$rank <- rank(-st.hd.st.ses.df$times,ties.method="min")
  st.hd.st.ses.df <- st.hd.st.ses.df[order(st.hd.st.ses.df$rank,decreasing = F),]
  st.hd.st.ses.df
  
  st.hd.st.ses.df.fin <- left_join(st.hd.st.ses.df,
                                  state.tracts,
                                  "state")
  
  st.hd.st.ses.df.fin$pct <- st.hd.st.ses.df.fin$times/st.hd.st.ses.df.fin$ntracts
  
  
  ## create table ## 
  
  tract.type.ses <- c("Persistently segregated",
                      "Persistently LI segregated",
                      "Persistenly MI segregated",
                      "Persistently HI segregated",
                      "Persistently multiple segregated",
                      "Integrated to segregated",
                      "Integrated to LI segregated",
                      "Integrated to MI segregated",
                      "Integrated to HI segregated",
                      "Integrated to multiple segregated",
                      "Segregated to integrated",
                      "Segregated to LI integrated",
                      "Segregated to MI integrated",
                      "Segregated to HI integrated",
                      "Segregated to multiple integrated",
                      "Persistently integrated",
                      "Persistently LI integrated",
                      "Persistently MI integrated",
                      "Persistently HI integrated",
                      "Persistently multiple integrated",
                      "Persistently highly diverse")
  
  dfs.ses <- list(stable.seg.tracts.ses,
                  stable.seg.li.tracts,
                  stable.seg.mi.tracts,
                  stable.seg.hi.tracts,
                  stable.seg.other.tracts,
                  int.to.seg.tracts.ses,
                  to.li.seg,
                  to.mi.seg,
                  to.hi.seg,
                  to.other.seg,
                  seg.to.int.tracts.ses,
                  to.li.int,
                  to.mi.int,
                  to.hi.int,
                  to.other.int,
                  stable.int.tracts.ses,
                  stable.int.li.tracts,
                  stable.int.mi.tracts,
                  stable.int.hi.tracts,
                  stable.int.other.tracts,
                  stable.hd.tracts)
  
  ## what other trajectories are missing? ##
  
  cb <- bind_rows(dfs.ses)
  
  missing <- indata %>%
    filter(GEOID %notin% cb$GEOID) %>%
    mutate(inv = paste0(get(var1),
                        get(var2),
                        get(var3)))
  
  print("OTHER TRAJECTORIES (INT = 1)")
  print(table(missing$inv, useNA = "ifany"))
  
  ## proceed ##
  
  num.ses <- sapply(dfs.ses, function(x) nrow(x))
  
  num.n.ses <- as.numeric(as.character(num.ses))
  
  t.stats.table.ses <- as.data.frame(cbind(tract.type.ses, num.n.ses))
  
  table(indata$int_er_rel_2020)
  
  seg <- table(indata$int_er_rel_2020)[0]
  
  int <- table(indata$int_er_rel_2020)[1]
  
  t.stats.table.pr.ses <- t.stats.table.ses %>%
    mutate(prev.by.type = num.ses/nrow(indata)) %>%
    mutate(prev.by.type = case_when(tract.type.ses %in% c("Persistently LI segregated",
                                                          "Persistenly MI segregated",
                                                          "Persistently HI segregated",
                                                          "Persistently multiple segregated") ~ num.ses/nrow(stable.seg.tracts.ses), 
                                    tract.type.ses %in% c("Integrated to LI segregated",
                                                          "Integrated to MI segregated",
                                                          "Integrated to HI segregated",
                                                          "Integrated to multiple segregated") ~ num.ses/nrow(int.to.seg.tracts.ses), 
                                    tract.type.ses %in% c("Segregated to LI integrated",
                                                          "Segregated to MI integrated",
                                                          "Segregated to HI integrated",
                                                          "Segregated to multiple integrated") ~ num.ses/nrow(seg.to.int.tracts.ses), 
                                    tract.type.ses %in% c("Persistently LI integrated",
                                                          "Persistently MI integrated",
                                                          "Persistently HI integrated",
                                                          "Persistently multiple integrated") ~ num.ses/nrow(stable.int.tracts.ses),
                                    TRUE ~ prev.by.type)) %>%
    mutate(prev.by.ses = case_when(str_detect(tract.type.ses, " LI") ~ num.ses/table(af.wide.final$maj_ses_2020)["li"],
                                   str_detect(tract.type.ses, " MI") ~ num.ses/table(af.wide.final$maj_ses_2020)["mi"],
                                   str_detect(tract.type.ses, " HI") ~ num.ses/table(af.wide.final$maj_ses_2020)["hi"])) %>%
    mutate(prev.by.type = as.numeric(format(prev.by.type, scientific = F)))
  
  t.stats.table.pr.ses$prev.by.type <- round(t.stats.table.pr.ses$prev.by.type, 3)
  t.stats.table.pr.ses$prev.by.ses <- round(t.stats.table.pr.ses$prev.by.ses, 3)
  
  return(t.stats.table.pr.ses)
  
}


ses.0020.prim <- ses.out(af.wide.final,
                      "int_ses_rel_a_2000",
                      "int_ses_rel_a_2010",
                      "int_ses_rel_a_2020",
                      "hd_ses_2000",
                      "hd_ses_2010",
                      "hd_ses_2020")

ses.0020.abs <- ses.out(af.wide.final,
                        "int_ses_2000",
                        "int_ses_2010",
                        "int_ses_2020",
                        "hd_ses_2000",
                        "hd_ses_2010",
                        "hd_ses_2020")

## final stats ##

## what's the breakdown of stably segregated white and black neighborhoods ##

st.seg.white <- af.wide.final %>%
  filter(maj_race_2000 == "per_white" & int_jt_relf_a_2000 == 0 & 
         maj_race_2010 == "per_white" & int_jt_relf_a_2010 == 0 & 
         maj_race_2020 == "per_white" & int_jt_relf_a_2020 == 0)

prop.table(table(st.seg.white$maj_ses_2020))

st.seg.black <- af.wide.final %>%
  filter(maj_race_2000 == "per_black" & int_jt_relf_a_2000 == 0 & 
           maj_race_2010 == "per_black" & int_jt_relf_a_2010 == 0 & 
           maj_race_2020 == "per_black" & int_jt_relf_a_2020 == 0)

prop.table(table(st.seg.black$maj_ses_2020))

to.int.white <- af.wide.final %>%
  filter((maj_race_2000 == "per_white" & int_jt_relf_a_2000 == 0 & 
          maj_race_2010 == "per_white" & int_jt_relf_a_2010 == 1 & 
          maj_race_2020 == "per_white" & int_jt_relf_a_2020 == 1) |
         (maj_race_2000 == "per_white" & int_jt_relf_a_2000 == 0 & 
          maj_race_2010 == "per_white" & int_jt_relf_a_2010 == 0 & 
          maj_race_2020 == "per_white" & int_jt_relf_a_2020 == 1))
           
prop.table(table(to.int.white$maj_ses_2020))

to.int.black <- af.wide.final %>%
  filter((maj_race_2000 == "per_black" & int_jt_relf_a_2000 == 0 & 
            maj_race_2010 == "per_black" & int_jt_relf_a_2010 == 1 & 
            maj_race_2020 == "per_black" & int_jt_relf_a_2020 == 1) |
           (maj_race_2000 == "per_black" & int_jt_relf_a_2000 == 0 & 
              maj_race_2010 == "per_black" & int_jt_relf_a_2010 == 0 & 
              maj_race_2020 == "per_black" & int_jt_relf_a_2020 == 1))

prop.table(table(to.int.black$maj_ses_2020))


#########################
## FULL JOINT ANALYSIS ##
#########################

all.jt.out <- function(indata, var1, var2, var3){
  
  ## 2000 integrated tracts ##
  
  integrated.trt.2000 <- indata %>%
    filter(get(var1) == 1) %>%
    select(GEOID)
  
  ## integrated tracts 2010 ## 
  
  integrated.trt.2010 <- indata %>%
    filter(get(var2) == 1) %>%
    select(GEOID)
  
  ## integrated tracts 2020 ##
  integrated.trt.2020 <- indata %>%
    filter(get(var3) == 1) %>%
    select(GEOID)
  
  ## stably segregated tracts ## 
  stable.seg.tracts <- filter(indata, 
                              get(var1) == 0,
                              get(var2) == 0,
                              get(var3) == 0)
  
  ## stably segregated White tracts ## 
  
  stable.seg.li.white.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_white" & 
             maj_race_2010 == "per_white" & 
             maj_race_2020 == "per_white" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.mi.white.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_white" & 
             maj_race_2010 == "per_white" & 
             maj_race_2020 == "per_white" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.hi.white.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_white" & 
             maj_race_2010 == "per_white" & 
             maj_race_2020 == "per_white" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated Black tracts ## 
  
  stable.seg.li.black.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_black" & 
             maj_race_2010 == "per_black" & 
             maj_race_2020 == "per_black" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.mi.black.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_black" & 
             maj_race_2010 == "per_black" & 
             maj_race_2020 == "per_black" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.hi.black.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_black" & 
             maj_race_2010 == "per_black" & 
             maj_race_2020 == "per_black" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated Latino tracts ## 
  
  stable.seg.li.latinx.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_hl" & 
             maj_race_2010 == "per_hl" & 
             maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.mi.latinx.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_hl" & 
             maj_race_2010 == "per_hl" & 
             maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.hi.latinx.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_hl" & 
             maj_race_2010 == "per_hl" & 
             maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably segregated Asian tracts ## 
  
  stable.seg.li.asian.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_asian" & 
             maj_race_2010 == "per_asian" & 
             maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.mi.asian.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_asian" & 
             maj_race_2010 == "per_asian" & 
             maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.seg.hi.asian.tracts <- stable.seg.tracts %>%
    filter(maj_race_2000 == "per_asian" & 
             maj_race_2010 == "per_asian" & 
             maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## all other stably segregated tracts ##
  
  stable.seg.other.tracts <- stable.seg.tracts %>%
    filter(GEOID %notin% c(stable.seg.li.asian.tracts$GEOID,
                           stable.seg.mi.asian.tracts$GEOID,
                           stable.seg.hi.asian.tracts$GEOID,
                           stable.seg.li.black.tracts$GEOID,
                           stable.seg.mi.black.tracts$GEOID,
                           stable.seg.hi.black.tracts$GEOID,
                           stable.seg.li.latinx.tracts$GEOID,
                           stable.seg.mi.latinx.tracts$GEOID,
                           stable.seg.hi.latinx.tracts$GEOID,
                           stable.seg.li.white.tracts$GEOID,
                           stable.seg.mi.white.tracts$GEOID,
                           stable.seg.hi.white.tracts$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  #print("MULTIPLE STABLE SEG NHS")
  #print(nrow(stable.seg.other.tracts))
  
  ## collect GEOIDS ## 
  seg.GEOIDs <- c(stable.seg.li.asian.tracts$GEOID,
                  stable.seg.mi.asian.tracts$GEOID,
                  stable.seg.hi.asian.tracts$GEOID,
                  stable.seg.li.black.tracts$GEOID,
                  stable.seg.mi.black.tracts$GEOID,
                  stable.seg.hi.black.tracts$GEOID,
                  stable.seg.li.latinx.tracts$GEOID,
                  stable.seg.mi.latinx.tracts$GEOID,
                  stable.seg.hi.latinx.tracts$GEOID,
                  stable.seg.li.white.tracts$GEOID,
                  stable.seg.mi.white.tracts$GEOID,
                  stable.seg.hi.white.tracts$GEOID,
                  stable.seg.other.tracts)
  
  check1 <- filter(stable.seg.tracts, 
                   GEOID %notin% seg.GEOIDs)
  
  ## tracts that went from integrated to segregated ## 
  int.to.seg.tracts <- indata %>%
    filter((get(var1) == 1 & get(var2) == 0 & get(var3) == 0) | 
             (get(var1) == 1 & get(var2) == 1 & get(var3) == 0))
  
  table(int.to.seg.tracts$maj_race_2000)
  table(int.to.seg.tracts$maj_race_2010)
  table(int.to.seg.tracts$maj_race_2020)
  
  ## transition to White segregated ## 
  
  to.wh.li.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_white" & maj_ses_2010 == "linc" & 
              get(var3) == 0 & maj_race_2020 == "per_white" & maj_ses_2020 == "linc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_white" &
                maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.wh.mi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_white" & maj_ses_2010 == "minc" & 
              get(var3) == 0 & maj_race_2020 == "per_white" & maj_ses_2020 == "minc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_white" &
                maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.wh.hi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_white" & maj_ses_2010 == "hinc" & 
              get(var3) == 0 & maj_race_2020 == "per_white" & maj_ses_2020 == "hinc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_white" &
                maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## transition to Black segregated ## 
  
  to.bl.li.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_black" & maj_ses_2010 == "linc" & 
              get(var3) == 0 & maj_race_2020 == "per_black" & maj_ses_2020 == "linc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_black" &
                maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.bl.mi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_black" & maj_ses_2010 == "minc" & 
              get(var3) == 0 & maj_race_2020 == "per_black" & maj_ses_2020 == "minc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_black" &
                maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  to.bl.hi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_black" & maj_ses_2010 == "hinc" & 
              get(var3) == 0 & maj_race_2020 == "per_black" & maj_ses_2020 == "hinc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_black" &
                maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## transition to Latinx segregated ## 
  
  to.lx.li.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_hl" & maj_ses_2010 == "linc" & 
              get(var3) == 0 & maj_race_2020 == "per_hl" & maj_ses_2020 == "linc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_hl" &
                maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.lx.mi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_hl" & maj_ses_2010 == "minc" & 
              get(var3) == 0 & maj_race_2020 == "per_hl" & maj_ses_2020 == "minc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_hl" &
                maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.lx.hi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_hl" & maj_ses_2010 == "hinc" & 
              get(var3) == 0 & maj_race_2020 == "per_hl" & maj_ses_2020 == "hinc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_hl" &
                maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## transition to Asian segregated ## 
  
  to.as.li.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_asian" & maj_ses_2010 == "linc" & 
              get(var3) == 0 & maj_race_2020 == "per_asian" & maj_ses_2020 == "linc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_asian" &
                maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.as.mi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_asian" & maj_ses_2010 == "minc" & 
              get(var3) == 0 & maj_race_2020 == "per_asian" & maj_ses_2020 == "minc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_asian" &
                maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  to.as.hi.seg <- int.to.seg.tracts %>%
    filter((get(var2) == 0 & maj_race_2010 == "per_asian" & maj_ses_2010 == "hinc" & 
              get(var3) == 0 & maj_race_2020 == "per_asian" & maj_ses_2020 == "hinc") |
             (get(var2) == 1 & get(var3) == 0 & maj_race_2020 == "per_asian" &
                maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## all other int to seg tracts ##
  
  to.oth.seg <- int.to.seg.tracts %>%
    filter(GEOID %notin% c(to.as.li.seg$GEOID,
                           to.as.mi.seg$GEOID,
                           to.as.hi.seg$GEOID,
                           to.bl.li.seg$GEOID,
                           to.bl.mi.seg$GEOID,
                           to.bl.hi.seg$GEOID,
                           to.lx.li.seg$GEOID,
                           to.lx.mi.seg$GEOID,
                           to.lx.hi.seg$GEOID,
                           to.wh.li.seg$GEOID,
                           to.wh.mi.seg$GEOID,
                           to.wh.hi.seg$GEOID)) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  #print("MULTIPLE TO SEG NHS")
  #print(nrow(to.oth.seg))
  
  ## collect GEOIDS ## 
  int.to.seg.GEOIDs <- c(to.as.li.seg$GEOID,
                         to.as.mi.seg$GEOID,
                         to.as.hi.seg$GEOID,
                         to.bl.li.seg$GEOID,
                         to.bl.mi.seg$GEOID,
                         to.bl.hi.seg$GEOID,
                         to.lx.li.seg$GEOID,
                         to.lx.mi.seg$GEOID,
                         to.lx.hi.seg$GEOID,
                         to.wh.li.seg$GEOID,
                         to.wh.mi.seg$GEOID,
                         to.wh.hi.seg$GEOID)
  
  check2 <- filter(int.to.seg.tracts, 
                   GEOID %notin% int.to.seg.GEOIDs)
  
  table(check2$maj_race_2000)
  table(check2$maj_race_2010)
  table(check2$maj_race_2020)
  
  ## tracts that went from segregated to integrated ## 
  seg.to.int.tracts <- indata %>%
    filter((get(var1) == 0 & get(var2) == 1 & get(var3) == 1) | 
             (get(var1) == 0 & get(var2) == 0 & get(var3) == 1))
  
  table(seg.to.int.tracts$maj_race_2000)
  table(seg.to.int.tracts$maj_race_2010)
  table(seg.to.int.tracts$maj_race_2020)
  
  ## transition to White integrated ## 
  
  to.wh.li.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_white" & maj_ses_2010 == "linc" &
              get(var3) == 1 & maj_race_2020 == "per_white" & 
              maj_ses_2020 == "linc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_white" & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.wh.mi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_white" & maj_ses_2010 == "minc" &
              get(var3) == 1 & maj_race_2020 == "per_white" & 
              maj_ses_2020 == "minc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_white" & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.wh.hi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_white" & maj_ses_2010 == "hinc" &
              get(var3) == 1 & maj_race_2020 == "per_white" & 
              maj_ses_2020 == "hinc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_white" & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## transition to Black integrated ## 
  
  to.bl.li.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_black" & maj_ses_2010 == "linc" &
              get(var3) == 1 & maj_race_2020 == "per_black" & 
              maj_ses_2020 == "linc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_black" & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.bl.mi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_black" & maj_ses_2010 == "minc" &
              get(var3) == 1 & maj_race_2020 == "per_black" & 
              maj_ses_2020 == "minc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_black" & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.bl.hi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_black" & maj_ses_2010 == "hinc" &
              get(var3) == 1 & maj_race_2020 == "per_black" & 
              maj_ses_2020 == "hinc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_black" & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## transition to Latinx integrated ## 

  to.lx.li.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_hl" & maj_ses_2010 == "linc" &
              get(var3) == 1 & maj_race_2020 == "per_hl" & 
              maj_ses_2020 == "linc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_hl" & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.lx.mi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_hl" & maj_ses_2010 == "minc" &
              get(var3) == 1 & maj_race_2020 == "per_hl" & 
              maj_ses_2020 == "minc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_hl" & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.lx.hi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_hl" & maj_ses_2010 == "hinc" &
              get(var3) == 1 & maj_race_2020 == "per_hl" & 
              maj_ses_2020 == "hinc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_hl" & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## transition to Asian integrated ## 
  
  to.as.li.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_asian" & maj_ses_2010 == "linc" &
              get(var3) == 1 & maj_race_2020 == "per_asian" & 
              maj_ses_2020 == "linc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_asian" & maj_ses_2020 == "linc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.as.mi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_asian" & maj_ses_2010 == "minc" &
              get(var3) == 1 & maj_race_2020 == "per_asian" & 
              maj_ses_2020 == "minc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_asian" & maj_ses_2020 == "minc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  to.as.hi.int <- seg.to.int.tracts %>%
    filter((get(var2) == 1 & maj_race_2010 == "per_asian" & maj_ses_2010 == "hinc" &
              get(var3) == 1 & maj_race_2020 == "per_asian" & 
              maj_ses_2020 == "hinc") |
             (get(var2) == 0 & get(var3) == 1 & maj_race_2020 == "per_asian" & maj_ses_2020 == "hinc")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## all other seg to int tracts ##
  
  to.oth.int <- seg.to.int.tracts %>%
    filter(GEOID %notin% c(to.as.li.int$GEOID,
                           to.as.mi.int$GEOID,
                           to.as.hi.int$GEOID,
                           to.bl.li.int$GEOID,
                           to.bl.mi.int$GEOID,
                           to.bl.hi.int$GEOID,
                           to.lx.li.int$GEOID,
                           to.lx.mi.int$GEOID,
                           to.lx.hi.int$GEOID,
                           to.wh.li.int$GEOID,
                           to.wh.mi.int$GEOID,
                           to.wh.hi.int$GEOID) & 
             maj_race_2000 %notin% c("per_aian","per_other") & 
             maj_race_2010 %notin% c("per_aian","per_other") & 
             maj_race_2020 %notin% c("per_aian","per_other")) %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## collect GEOIDS ## 
  seg.to.int.GEOIDs <- c(to.as.li.int$GEOID,
                         to.as.mi.int$GEOID,
                         to.as.hi.int$GEOID,
                         to.bl.li.int$GEOID,
                         to.bl.mi.int$GEOID,
                         to.bl.hi.int$GEOID,
                         to.lx.li.int$GEOID,
                         to.lx.mi.int$GEOID,
                         to.lx.hi.int$GEOID,
                         to.wh.li.int$GEOID,
                         to.wh.mi.int$GEOID,
                         to.wh.hi.int$GEOID)
  
  check3 <- filter(seg.to.int.tracts, 
                   GEOID %notin% seg.to.int.GEOIDs)
  
  table(check3$maj_race_2000)
  table(check3$maj_race_2010)
  table(check3$maj_race_2020)
  
  ## stably integrated tracts ## 
  
  stable.int.tracts <- filter(indata, 
                              get(var1) == 1 & 
                              get(var2) == 1 & 
                              get(var3) == 1)
  
  table(stable.int.tracts$maj_race_2000)
  table(stable.int.tracts$maj_race_2010)
  table(stable.int.tracts$maj_race_2020)
  
  ## stably integrated White tracts ## 
  
  stable.int.li.white.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_white" & 
           maj_race_2010 == "per_white" & 
           maj_race_2020 == "per_white" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.mi.white.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_white" & 
           maj_race_2010 == "per_white" & 
           maj_race_2020 == "per_white" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.hi.white.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_white" & 
           maj_race_2010 == "per_white" & 
           maj_race_2020 == "per_white" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## stably integrated Black tracts ## 
  
  stable.int.li.black.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_black" &
           maj_race_2010 == "per_black" &
           maj_race_2020 == "per_black" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.mi.black.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_black" &
           maj_race_2010 == "per_black" &
           maj_race_2020 == "per_black" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  stable.int.hi.black.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_black" &
           maj_race_2010 == "per_black" &
           maj_race_2020 == "per_black" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## stably integrated Latinx tracts ## 

  stable.int.li.latinx.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_hl" &
           maj_race_2010 == "per_hl" &
           maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.mi.latinx.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_hl" &
           maj_race_2010 == "per_hl" &
           maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  stable.int.hi.latinx.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_hl"&
           maj_race_2010 == "per_hl" &
           maj_race_2020 == "per_hl" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  ## stably integrated Asian tracts ## 
  
  stable.int.li.asian.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_asian"&
           maj_race_2010 == "per_asian"&
           maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "linc" & 
             maj_ses_2010 == "linc" & 
             maj_ses_2020 == "linc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.mi.asian.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_asian"&
           maj_race_2010 == "per_asian" &
           maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "minc" & 
             maj_ses_2010 == "minc" & 
             maj_ses_2020 == "minc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  stable.int.hi.asian.tracts <- stable.int.tracts %>%
    filter(maj_race_2000 == "per_asian"&
           maj_race_2010 == "per_asian"&
           maj_race_2020 == "per_asian" & 
             maj_ses_2000 == "hinc" & 
             maj_ses_2010 == "hinc" & 
             maj_ses_2020 == "hinc") %>%
    select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))

  ## all other stably int tracts ##
  
  stable.int.other.tracts <- stable.int.tracts %>%
    filter(GEOID %notin% c(stable.int.li.asian.tracts$GEOID,
                           stable.int.mi.asian.tracts$GEOID,
                           stable.int.hi.asian.tracts$GEOID,
                           stable.int.li.black.tracts$GEOID,
                           stable.int.mi.black.tracts$GEOID,
                           stable.int.hi.black.tracts$GEOID,
                           stable.int.li.latinx.tracts$GEOID,
                           stable.int.mi.latinx.tracts$GEOID,
                           stable.int.hi.latinx.tracts$GEOID,
                           stable.int.li.white.tracts$GEOID,
                           stable.int.mi.white.tracts$GEOID,
                           stable.int.hi.white.tracts$GEOID) & 
             maj_race_2000 %notin% c("per_aian","per_other") & 
             maj_race_2010 %notin% c("per_aian","per_other") & 
             maj_race_2020 %notin% c("per_aian","per_other")) %>%
    #select(GEOID) %>%
    mutate(FIPS = substr(GEOID,1,2))
  
  stable.int.routes <- stable.int.other.tracts %>%
    group_by(maj_race_2000,
             maj_race_2010,
             maj_race_2020,
             maj_ses_2000,
             maj_ses_2010,
             maj_ses_2020) %>%
    summarize(tot = n())
  
  
  
  out.dfs <- list(stable.seg.li.asian.tracts,
                  stable.seg.mi.asian.tracts,
                  stable.seg.hi.asian.tracts,
                  stable.seg.li.black.tracts,
                  stable.seg.mi.black.tracts,
                  stable.seg.hi.black.tracts,
                  stable.seg.li.latinx.tracts,
                  stable.seg.mi.latinx.tracts,
                  stable.seg.hi.latinx.tracts,
                  stable.seg.li.white.tracts,
                  stable.seg.mi.white.tracts,
                  stable.seg.hi.white.tracts,
                  to.as.li.seg,
                  to.as.mi.seg,
                  to.as.hi.seg,
                  to.bl.li.seg,
                  to.bl.mi.seg,
                  to.bl.hi.seg,
                  to.lx.li.seg,
                  to.lx.mi.seg,
                  to.lx.hi.seg,
                  to.wh.li.seg,
                  to.wh.mi.seg,
                  to.wh.hi.seg,
                  to.as.li.int,
                  to.as.mi.int,
                  to.as.hi.int,
                  to.bl.li.int,
                  to.bl.mi.int,
                  to.bl.hi.int,
                  to.lx.li.int,
                  to.lx.mi.int,
                  to.lx.hi.int,
                  to.wh.li.int,
                  to.wh.mi.int,
                  to.wh.hi.int,
                  stable.int.li.asian.tracts,
                  stable.int.mi.asian.tracts,
                  stable.int.hi.asian.tracts,
                  stable.int.li.black.tracts,
                  stable.int.mi.black.tracts,
                  stable.int.hi.black.tracts,
                  stable.int.li.latinx.tracts,
                  stable.int.mi.latinx.tracts,
                  stable.int.hi.latinx.tracts,
                  stable.int.li.white.tracts,
                  stable.int.mi.white.tracts,
                  stable.int.hi.white.tracts)


  return(out.dfs)
  
}



all.jt.res <- all.jt.out(af.wide.final,
                       "int_jt_relf_a_2000",
                       "int_jt_relf_a_2010",
                       "int_jt_relf_a_2020")

## 
## create data for figure 
##

row.counts <- function(x) {
  
  print(nrow(x))
}

nrows <- sapply(all.jt.res, row.counts)

pdatas <- data.frame(group = c("stable seg Asian LI",
                               "stable seg Asian MI",
                               "stable seg Asian HI",
                               "stable seg Black LI",
                               "stable seg Black MI",
                               "stable seg Black HI",
                               "stable seg Hispanic/Latino LI",
                               "stable seg Hispanic/Latino MI",
                               "stable seg Hispanic/Latino HI",
                               "stable seg White LI",
                               "stable seg White MI",
                               "stable seg White HI",
                               "to seg Asian LI",
                               "to seg Asian MI",
                               "to seg Asian HI",
                               "to seg Black LI",
                               "to seg Black MI",
                               "to seg Black HI",
                               "to seg Hispanic/Latino LI",
                               "to seg Hispanic/Latino MI",
                               "to seg Hispanic/Latino HI",
                               "to seg White LI",
                               "to seg White MI",
                               "to seg White HI",
                               "to int Asian LI",
                               "to int Asian MI",
                               "to int Asian HI",
                               "to int Black LI",
                               "to int Black MI",
                               "to int Black HI",
                               "to int Hispanic/Latino LI",
                               "to int Hispanic/Latino MI",
                               "to int Hispanic/Latino HI",
                               "to int White LI",
                               "to int White MI",
                               "to int White HI",
                               "stable int Asian LI",
                               "stable int Asian MI",
                               "stable int Asian HI",
                               "stable int Black LI",
                               "stable int Black MI",
                               "stable int Black HI",
                               "stable int Hispanic/Latino LI",
                               "stable int Hispanic/Latino MI",
                               "stable int Hispanic/Latino HI",
                               "stable int White LI",
                               "stable int White MI",
                               "stable int White HI"),
                     num = nrows)

## capture trajectories with more than 100 neighborhoods ##

pdata.m100 <- pdatas %>%
  filter(num > 100)

pdatas.geoids <- bind_rows(all.jt.res)
length(unique(pdatas.geoids$GEOID)) == nrow(pdatas.geoids)

rest <- af.wide.final %>%
  filter(GEOID %notin% pdatas.geoids$GEOID) %>% 
  group_by(int_jt_relf_a_2000,
           int_jt_relf_a_2010,
           int_jt_relf_a_2020,
           maj_race_2000,
           maj_race_2010,
           maj_race_2020,
           maj_ses_2000,
           maj_ses_2010,
           maj_ses_2020) %>%
  summarize(n=n())

## compute the remaining trajectories ##

rest.t100 <- rest %>%
  filter(n > 100) %>%
  mutate(group = case_when(# r1 #
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "stable seg white hi to li",
                    # r2 #
                    int_jt_relf_a_2000 == 0 & 
                      int_jt_relf_a_2010 == 0 & 
                      int_jt_relf_a_2020 == 0 & 
                      maj_race_2000 == "per_white" & 
                      maj_race_2010 == "per_white" & 
                      maj_race_2020 == "per_white" & 
                      maj_ses_2000 == "linc" & 
                      maj_ses_2010 == "hinc" & 
                      maj_ses_2020 == "hinc" ~ "stable seg white li to hi",
                    # r3 # 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "stable seg white li to li",
                    ## r4 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "stable seg white li to hi",
                    ## r5 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable seg White hi to li",
                    ## r6 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "stable seg white hi to hi",
                    ## r7 ## 
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int li white to Hispanic/Latino",
                    ## r8 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "seg white hi to seg white hi",
                    ## r9 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "seg white hi to int white li",
                    ## r10 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "seg white li to seg white li",
                    ## r11 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int linc white to Hispanic/Latino",
                    ## r12 ## 
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "stable int white hi to li",
                    ## r13 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "stable int white li to hi",
                    ## r14 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int white hi to li",
                    ## r15 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "seg white li to int white li",
                    ## r16 ## 
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "linc" ~ "stable int white li to li",
                    ## r17 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "stable int hi white to Hispanic/Latino",
                    ## r18 ## 
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "stable int hi white to Hispanic/Latino",
                    ## r19 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_black" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable seg li black to Hispanic/Latino",
                    ## r20 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_black" & 
                    maj_race_2010 == "per_black" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable seg li black to Hispanic/Latino",
                    ## r21 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_black" & 
                    maj_race_2020 == "per_black" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int li white to black",
                    ## r22 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable seg li white to Hispanic/Latino",
                    ## r23 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "seg li white to int hi white",
                    ## r24 ##
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "seg white hi to int hi white",
                    ## r25 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_asian" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "stable seg hi white to asian",
                    ## r26 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "stable int white li to hi",
                    ## r27 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int white li to li",
                    ## r28 ## 
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "stable int hi white to li latino",
                    ## r29 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_black" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable int li white to black",
                    ## r30 ##
                    int_jt_relf_a_2000 == 1 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 1 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_white" & 
                    maj_race_2020 == "per_white" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "hinc" ~ "stable int white hi to hi",
                    ## r31 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_black" & 
                    maj_race_2010 == "per_black" & 
                    maj_race_2020 == "per_black" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "stable seg black hi to li",
                    ## r32 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 1 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_hl" & 
                    maj_race_2010 == "per_hl" & 
                    maj_race_2020 == "per_hl" & 
                    maj_ses_2000 == "linc" & 
                    maj_ses_2010 == "linc" & 
                    maj_ses_2020 == "linc" ~ "seg li latino to seg li latino",
                    ## r32 ## 
                    int_jt_relf_a_2000 == 0 & 
                    int_jt_relf_a_2010 == 0 & 
                    int_jt_relf_a_2020 == 0 & 
                    maj_race_2000 == "per_white" & 
                    maj_race_2010 == "per_asian" & 
                    maj_race_2020 == "per_asian" & 
                    maj_ses_2000 == "hinc" & 
                    maj_ses_2010 == "hinc" & 
                    maj_ses_2020 == "hinc" ~ "stable seg hi white to asian"))

rest.fin <- rest.t100 %>%
  group_by(group) %>%
  summarize(num = sum(n,na.rm=T)) %>%
  filter(!is.na(group))

(sum(rest.t100$n) + nrow(pdatas.geoids))/nrow(af.wide.final)

## combine all the trajectories for the figure ##

all.tracks <- rbind(pdata.m100,
                    rest.fin) %>%
  group_by(group) %>%
  summarize(num = sum(num)) %>%
  mutate(pct = round(num/sum(num)*100,1),
         parent = case_when(grepl("stable seg", group)~ "Stable Segregation",
                            grepl("to seg", group)~"Int to Seg",
                            grepl("to int", group)~"Seg to Int",
                            grepl("stable int", group)~"Stable Integration"),
         group_new = trim(removeWords(group,c("stable seg",
                                              "stable int",
                                              "to seg",
                                              "to int"))))

all.tracks$group_new[all.tracks$group_new == "white li to hi"] <- "White LI to HI"
all.tracks$group_new[all.tracks$group_new == "white hi to li"] <- "White HI to LI"


sum(all.tracks$num)/nrow(af.wide.final)

prop.table(table(af.wide.final$int_jt_relf_a_2010[af.wide.final$int_jt_relf_a_2000 == 1]))
prop.table(table(af.wide.final$int_jt_relf_a_2020[af.wide.final$int_jt_relf_a_2010 == 1]))


sum(all.tracks$num[all.tracks$parent == "Seg to Int"])/(sum(all.tracks$num[all.tracks$parent == "Seg to Int"]) + sum(all.tracks$num[all.tracks$parent == "Int to Seg"]))
sum(all.tracks$num[all.tracks$parent == "Int to Seg"])/(sum(all.tracks$num[all.tracks$parent == "Seg to Int"]) + sum(all.tracks$num[all.tracks$parent == "Int to Seg"]))

## figure 5 ##

PieDonut(all.tracks, 
         aes(pies = parent, donuts = group_new, count = pct),
         ratioByGroup=F,
         addDonutLabel = F,
         #use.labels = F,
         showPieName=FALSE) 

sum(rest$n) + sum(pdatas$num) == nrow(af.wide.final)

##
## how much of the sample are we missing from fig 4 ##
##

pdatas.b100 <- pdatas %>%
  filter(num <= 100)

sum(pdatas.b100$num)

rest.b100 <- rest %>%
  filter(n <= 100)

## lets start with pdatas ## 

## no trajectory ##
pdatas.b100.wild <- pdatas.b100 %>%
  filter(grepl("to seg|to int",group))

sum(pdatas.b100.wild$num)

## stable trajectory ##
pdatas.b100.stab <- pdatas.b100 %>%
  filter(!grepl("to seg|to int",group))

sum(pdatas.b100.stab$num)

## check to make sure obs line up ##
sum(pdatas.b100.wild$num) + sum(pdatas.b100.stab$num) == sum(pdatas.b100$num)


rest.b100.wild <- rest.b100 %>%
  filter((int_jt_relf_a_2000 == 0 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 0) | 
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 0 & int_jt_relf_a_2020 == 1) |
         (int_jt_relf_a_2000 == 0 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 1) | 
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 0) |
         (int_jt_relf_a_2000 == 0 & int_jt_relf_a_2010 == 0 & int_jt_relf_a_2020 == 1) | 
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 0 & int_jt_relf_a_2020 == 0))

sum(rest.b100.wild$n)

rest.b100.stab <- rest.b100 %>%
  filter((int_jt_relf_a_2000 == 0 & int_jt_relf_a_2010 == 0 & int_jt_relf_a_2020 == 0) | 
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 1))

## check to make sure obs line up ##
sum(rest.b100.stab$n) + sum(rest.b100.wild$n) == sum(rest.b100$n)
sum(rest.b100.stab$n) + sum(pdatas.b100.stab$num) + sum(rest.b100.wild$n) + sum(pdatas.b100.wild$num) == sum(rest.b100$n) + sum(pdatas.b100$num)

## also just those to seg ## 

pdatas.b100.wildseg <- pdatas.b100 %>%
  filter(grepl("to seg",group))

rest.b100.wildseg <- rest.b100 %>%
  filter((int_jt_relf_a_2000 == 0 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 0) | 
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 1 & int_jt_relf_a_2020 == 0) |
         (int_jt_relf_a_2000 == 1 & int_jt_relf_a_2010 == 0 & int_jt_relf_a_2020 == 0))

## final answers ##

(sum(rest.b100.wild$n) + sum(pdatas.b100.wild$num))/nrow(af.wide.final) ## tracts with no definite trajectory 
(sum(rest.b100.wildseg$n) + sum(pdatas.b100.wildseg$num))/nrow(af.wide.final) ## subset of the unstable trajectory (those that ended in segregation)
(sum(rest.b100.stab$n) + sum(pdatas.b100.stab$num))/nrow(af.wide.final) ## tracts that were stable


## explore trajectories in more detail ## 

int.2000 <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 1)

## share of int neighborhoods in 2000 that were int in 2010 ##
prop.table(table(int.2000$int_jt_relf_a_2010))

int.2010 <- af.wide.final %>%
  filter(int_jt_relf_a_2010 == 1)

## share of int neighborhoods in 2010 that were int in 2020 ##
prop.table(table(int.2010$int_jt_relf_a_2020))

stab.int.tracks <- all.tracks %>%
  filter(parent == "Stable Integration")

stab.int <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 1 & 
         int_jt_relf_a_2010 == 1 & 
         int_jt_relf_a_2020 == 1)

prop.table(table(stab.int$maj_ses_2000))
prop.table(table(stab.int$maj_ses_2010))
prop.table(table(stab.int$maj_ses_2020))

stab.seg.tracks <- all.tracks %>%
  filter(parent == "Stable Segregation")

## average economic profile of stably segregated white neighborhoods ##

stab.seg.wh <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 0 & 
         int_jt_relf_a_2010 == 0 & 
         int_jt_relf_a_2020 == 0 & 
         maj_race_2000 == "per_white" & 
         maj_race_2010 == "per_white" & 
         maj_race_2020 == "per_white")

summary(stab.seg.wh$median_hhld_inc_2020)
summary(stab.seg.wh$hhld_pov_rt_2020)

## average economic profile of stably segregated black neighborhoods ##

stab.seg.bl <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 0 & 
           int_jt_relf_a_2010 == 0 & 
           int_jt_relf_a_2020 == 0 & 
           maj_race_2000 == "per_black" & 
           maj_race_2010 == "per_black" & 
           maj_race_2020 == "per_black")

summary(stab.seg.bl$median_hhld_inc_2020)
summary(stab.seg.bl$hhld_pov_rt_2020)



int.to.seg.tracks <-  all.tracks %>%
  filter(parent == "Int to Seg")

seg.to.int.tracks <-  all.tracks %>%
  filter(parent == "Seg to Int")

## other checks ##
prop.table(table(af.wide.final$maj_ses_2020))

stab.seg <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 0 & 
         int_jt_relf_a_2010 == 0 & 
         int_jt_relf_a_2020 == 0 )

prop.table(table(stab.seg$maj_ses_2000[stab.seg$maj_race_2000 == "per_black"]))
prop.table(table(stab.seg$maj_ses_2000[stab.seg$maj_race_2010 == "per_black"]))
prop.table(table(stab.seg$maj_ses_2000[stab.seg$maj_race_2020 == "per_black"]))

### END OF PROGRAM ###

sink()
sink(type = "message")